<template>
  <div>
    <el-dialog
      :title="eject.isadd == true ? '菜单添加' : '菜单编辑'"
      :visible.sync="eject.isshow"
      @closed="guanbi"
    >
      <el-form :model="form">
        <el-form-item label="菜单名称" label-width="120px" :rules="rules">
          <el-input v-model="form.title" autocomplete="off" required></el-input>
        </el-form-item>

        <el-form-item label="上级菜单" label-width="120px">
          <el-select v-model="form.pid" @change="changepid">
            <el-option disabled label="请选择" value></el-option>

            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in list"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单类型" label-width="120px">
          <el-radio :label="1" v-model="form.type" disabled>目录</el-radio>
          <el-radio :label="2" v-model="form.type" disabled>菜单</el-radio>
        </el-form-item>

        <el-form-item
          label="菜单图标"
          v-if="form.type == 1"
          label-width="120px"
        >
          <el-select v-model="form.icon">
            <el-option value disabled>请选择</el-option>
            <el-option value="el-icon-s-tools">
              <i class="el-icon-s-tools"></i>
            </el-option>
            <el-option value="el-icon-user-solid">
              <i class="el-icon-user-solid"></i>
            </el-option>
            <el-option value="el-icon-s-goods">
              <i class="el-icon-s-goods"></i>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单地址" v-else label-width="120px">
          <el-select v-model="form.url">
            <el-option disabled value>请选择1</el-option>
            <el-option
              v-for="item in IndexRouter"
              :key="item.path"
              :label="item.meta.title"
              :value="'/' + item.path"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inaactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="add()" v-if="eject.isadd"
          >添 加</el-button
        >
        <el-button type="primary" @click="update" v-else>编 辑</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { IndexRouter } from "../../../router/index.js";
import { reqmenuadd, reqmenuone, reqmenuedit } from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["eject", "list"],
  data() {
    return {
      form: {
        pid: "",
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      },
      icons: [
        "el-icon-s-tools",
        "el-icon-user-solid",
        "el-icon-s-goods",
        "el-icon-help",
        "el-icon-s-operation",
      ],
      IndexRouter,
      rules: [
        {
          required: true,
        },
      ],
    };
  },
  created() {},
  mounted() {},
  methods: {
    guanbi() {
      !this.eject.isadd && this.empty();
    },
    close() {
      this.$emit("close");
    },
    // 封装一个清空方法
    empty() {
      this.form = {
        pid: "",
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      };
    },
    //点击添加按钮
    add() {
      // console.log(this.form);
      this.$emit("add");
      // 发起请求
      reqmenuadd(this.form).then((res) => {
        console.log(res);
        // 添加成功
        if (res.data.code == 200) {
          // 弹出成功
          successAlert(res.data.msg);
          //清空输入框
          this.empty();
          // 弹框消失
          this.eject.isshow = false;
          // 重新列表请求
          this.$emit("init");
        }
      });

      // 弹框消失
      // 列表重新请求数据渲染
    },
    update() {
      // 请求接口
      reqmenuedit(this.form).then((res) => {
        console.log(res);
        if (res.data.code == 200) {
          // 弹出成功提示框
          successAlert(res.data.msg);
          // 清空输入框
          this.empty();
          // 关闭弹窗
          this.eject.isshow = false
          // 通知父组件更新列表
          this.$emit('update')
        }
      });
    },
    changepid() {
      this.form.type = this.form.pid == 0 ? 1 : 2;
    },
    // 获取一条信息方法，父组件通过ref调用
    getone(id) {
      console.log({ id: id });
      reqmenuone({ id: id }).then((res) => {
        console.log(res);
        this.form = res.data.list;
        //********************************* */
        this.form.id = id;
      });
    },
  },
};
</script>

<style scoped >
</style>
